﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Messaging;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace EuroAudit
{
    public partial class Tasks : System.Web.UI.Page
    {
        static InfoListSent ilsMsg = new InfoListSent();
        static InfoListReceived ilrMsg = new InfoListReceived();
        static TaskListSent tlsTask = new TaskListSent();
        static TaskListReceived tlrTask = new TaskListReceived();

        GlobalF GFunc = new GlobalF();
        static int ActiveTab;
        static string ViewedTaskID = null;//identifikators skatāmajam uzdevumam

        protected void Page_Load(object sender, EventArgs e)
        {
            GFunc.InitO();
            if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx", true);
                //return;
            }
            if (Session["UserName"].ToString() == "" | Session["UserName"].ToString() == "guest")
            {
                Response.Redirect("Login.aspx", true);
                //return;
            }
            //string sUName = Session["UserName"].ToString();
            //string sQry = "UPDATE Tasks SET TaskNotify = CONCAT(TaskNotify,';"+sUName+";') WHERE TaskTo like '%;" + sUName + ";%' and TaskNotify not like '%;" + sUName + ";%'";

            if (IsPostBack)
            {
                if (_RESULT.Text == "OKcmdDelTask")
                {
                    cmdDelTask_Click(sender, e);
                    _RESULT.Text = "";
                }
                if (_RESULT.Text == "OKcmdDelMsg")
                {
                    cmdDelMsg_Click(sender, e);
                    _RESULT.Text = "";
                }
            }
            //listbox'us aizpildam tikai pie pirmās ielādes
            if (!Page.IsPostBack)
            {
                //pievienojam operatorus filtrēšanas operatoru sarakstam
                ddlDarbFilt.Items.Clear();
                ListItem liOper = new ListItem();
                liOper.Text = "=";
                liOper.Value = "=";
                liOper.Selected = true;
                ddlDarbFilt.Items.Add(liOper);
                liOper = new ListItem();
                liOper.Text = "<";
                liOper.Value = "<";
                liOper.Selected = false;
                ddlDarbFilt.Items.Add(liOper);
                liOper = new ListItem();
                liOper.Text = ">";
                liOper.Value = ">";
                liOper.Selected = false;
                ddlDarbFilt.Items.Add(liOper);
                liOper = new ListItem();
                liOper.Text = "<=";
                liOper.Value = "<=";
                liOper.Selected = false;
                ddlDarbFilt.Items.Add(liOper);
                liOper = new ListItem();
                liOper.Text = ">=";
                liOper.Value = ">=";
                liOper.Selected = false;
                ddlDarbFilt.Items.Add(liOper);
                liOper = new ListItem();
                liOper.Text = "<>";
                liOper.Value = "<>";
                liOper.Selected = false;
                ddlDarbFilt.Items.Add(liOper);
                liOper = new ListItem();
                liOper.Text = "LIKE";
                liOper.Value = "LIKE";
                liOper.Selected = false;
                ddlDarbFilt.Items.Add(liOper);
                //pievienojam lauku nosaukumus 
                ddlFieldFilt.Items.Clear();
                string qrFields = "SELECT * FROM tasks LIMIT 1";
                DataSet dsFields = GFunc.GetData(qrFields, "WebCNS", false);
                if (dsFields.Tables.Count > 0)
                {
                    if (dsFields.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < dsFields.Tables[0].Columns.Count; i++)
                        {
                            liOper = new ListItem();
                            liOper.Text = dsFields.Tables[0].Columns[i].ColumnName;
                            liOper.Value = dsFields.Tables[0].Columns[i].ColumnName;
                            liOper.Selected = false;
                            ddlFieldFilt.Items.Add(liOper);
                        }
                    }
                }

                ddlFieldSort.Items.Clear();
                if (dsFields.Tables.Count > 0)
                {
                    if (dsFields.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < dsFields.Tables[0].Columns.Count; i++)
                        {
                            liOper = new ListItem();
                            liOper.Text = dsFields.Tables[0].Columns[i].ColumnName;
                            liOper.Value = dsFields.Tables[0].Columns[i].ColumnName;
                            liOper.Selected = false;
                            ddlFieldSort.Items.Add(liOper);
                        }
                    }
                }
            }
            if (ActiveTab == 0)
            {
                LoadMsgR();
                if (pOptions.Visible == true)
                {
                    ViewOptions("MsgReceived");
                }
            }
            if (ActiveTab == 1)
            {
                LoadMsgS();
                if (pOptions.Visible == true)
                {
                    ViewOptions("MsgSent");
                }
            }
            if (ActiveTab == 2)
            {
                LoadTasksR();
                if (pTskDetails.Visible == true)
                {
                    if (ViewedTaskID != null)
                    {
                        //pTskDetails.Visible = false;
                        ViewReceivedTask(Convert.ToInt32(ViewedTaskID));
                        upTskDetails.Update();

                    }
                }
                if (pOptions.Visible == true)
                {
                    ViewOptions("TskReceived");
                }
            }
            if (ActiveTab == 3)
            {
                LoadTasksS();
                if (pTskDetails.Visible == true)
                {
                    if (ViewedTaskID != null)
                    {
                        //pTskDetails.Visible = false;
                        ViewSentTask(Convert.ToInt32(ViewedTaskID));
                        upTskDetails.Update();

                    }
                }
                if (pOptions.Visible == true)
                {
                    ViewOptions("TskSent");
                }
            }
            
        }
        //public IEnumerable<InfoReceived> SortedList(string FieldsNames)
        //{
        //    /*var QrResults = from InfoReceived InfR in ilrMsg
        //                    orderby InfR.From descending
        //                    select InfR;
        //    */
        //    /*var QrR = from InfoReceived InfR in ilrMsg
        //              orderby ("ID")
        //              select InfR;*/
            
        //    var QrR =ilrMsg.AsQueryable().OrderBy("From").Select("New(ID,From,To)");
        //    /*var QrR = from InfoReceived InfR in ilrMsg
        //                    orderby ("InfR.ID")
        //                    select InfR;*/
        //    return (IEnumerable<InfoReceived>)QrR;
            
        //}
        
        
        //saņemtie ziņojumi
        public void LoadMsgR()
        {
            cmdListMsgIn.CssClass = "cmdTabsActive";
            cmdListMsgOut.CssClass = "cmdTabs";
            cmdListTasksOut.CssClass = "cmdTabs";
            cmdListTasksIn.CssClass = "cmdTabs";

            tList.Visible = true;
            gvList.Visible = false;
            tList.Rows.Clear();

            ilrMsg.ConnectionString = GFunc.GetConnectionString("WebCNS");
            ilrMsg.LoadData(Session["UserName"].ToString());
            /*//pieprasām klases laukus, kas tiks parādīti formā
            Type ilrType = (typeof(InfoReceived));
            PropertyInfo[] ilrProp = ilrType.GetProperties(BindingFlags.Public | BindingFlags.Instance);// | BindingFlags.Instance
            gvList.Columns.Clear();
            for (int i = 0; i < ilrProp.Length; i++)
            {
                //string pt = ilrProp[i].MemberType.ToString();
                if (ilrProp[i].PropertyType.IsSealed == true)
                {
                    BoundField BF = new BoundField();
                    BF.DataField = ilrProp[i].Name;
                    BF.HeaderText = ilrProp[i].Name;
                    //BF.SortExpression = ilrProp[i].Name;
                    gvList.Columns.Add(BF);
                }
            }
            gvList.DataSource = ilrMsg;
            gvList.DataBind();
             * 
             * GridViewRow gvr1 = gvList.Rows[i];
                    TableCell tc1 = new TableCell();
                    Button btnView = new Button();
                    btnView.Click += new EventHandler(btnView_Click);
                    btnView.ID = "cmdView" + gvList.Rows[i].Cells[1].Text;
                    btnView.Text = "Skatīt";
                    tc1.Controls.Add(btnView);
                    gvr1.Cells.AddAt(0, tc1);
             */
            foreach (InfoReceived MRcv in ilrMsg)
            //foreach (InfoReceived MRcv in (InfoListReceived)QrR)
            {
                if (!MRcv.MsgNotified)
                {
                    MRcv.MarkAsNotified(GFunc.GetConnectionString("WebCNS"), Session["UserName"].ToString());
                }
                Table tNest = new Table();
                TableRow trList = new TableRow();
                TableCell tcList = new TableCell();
                TableRow trNest = new TableRow();
                TableCell tcNest = new TableCell();
                Button btnView = new Button();
                btnView.Click += new EventHandler(btnView_Click);
                btnView.ID = "cmdView" + MRcv.ID;
                btnView.CommandArgument = MRcv.ID.ToString();
                btnView.PostBackUrl = "#pMsgDetails";
                btnView.CssClass = "cmdEdit";
                btnView.Text = "Skatīt";
                Label BkmListMRcv = new Label();
                BkmListMRcv.Text = "<a name=\"" + MRcv.ID.ToString() + "\"></a>";
                tcNest.Controls.Add(btnView);
                tcNest.Controls.Add(BkmListMRcv);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = "<B>No: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.From + " " + MRcv.Time;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                /*trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Termiņš: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.EndTime.ToString();
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Izpildīts: </B>";
                if (MRcv.CompletedTime.CompareTo(DateTime.Parse("01.01.1900")) > 0)
                {
                    tcNest.Text = tcNest.Text + " " + MRcv.CompletedTime;
                }
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                */
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Ziņojums: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.Name;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.Detail;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                /*trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Atskaite: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.Report;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                 */
                
                tcList.Controls.Add(tNest);
                if (MRcv.MsgRead)
                {
                    tcList.CssClass = "ReadTask";
                }
                else
                {
                    tcList.CssClass = "UnreadTask";
                }

                trList.Cells.Add(tcList);
                tList.Rows.Add(trList);
            }
        }
        //nosūtītie ziņojumi
        public void LoadMsgS()
        {
            cmdListMsgIn.CssClass = "cmdTabs";
            cmdListMsgOut.CssClass = "cmdTabsActive";
            cmdListTasksOut.CssClass = "cmdTabs";
            cmdListTasksIn.CssClass = "cmdTabs";

            tList.Visible = true;
            gvList.Visible = false;
            tList.Rows.Clear();

            ilsMsg.ConnectionString = GFunc.GetConnectionString("WebCNS");
            ilsMsg.LoadData(Session["UserName"].ToString());
            /*//pieprasām klases laukus, kas tiks parādīti formā
            Type ilsType = (typeof(InfoSent));
            PropertyInfo[] ilsProp = ilsType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            gvList.Columns.Clear();
            for (int i = 0; i < ilsProp.Length; i++)
            {
                if (ilsProp[i].PropertyType.IsSealed == true)
                {
                    BoundField BF = new BoundField();
                    BF.DataField = ilsProp[i].Name;
                    BF.HeaderText = ilsProp[i].Name;
                    //BF.SortExpression = ilsProp[i].Name;
                    gvList.Columns.Add(BF);
                }
            }
            gvList.DataSource = ilsMsg;
            gvList.DataBind();
             * 
             * GridViewRow gvr = gvList.Rows[i];
                    TableCell tc = new TableCell();
                    Button btn = new Button();
                    btn.Click += new EventHandler(btn_Click);
                    btn.ID = "cmdEdit" + gvList.Rows[i].Cells[1].Text;
                    btn.Text = "Labot";
                    tc.Controls.Add(btn);
                    gvr.Cells.AddAt(0, tc);
                    //skatīt
                    GridViewRow gvr1 = gvList.Rows[i];
                    TableCell tc1 = new TableCell();
                    Button btnView = new Button();
                    btnView.Click += new EventHandler(btnView_Click);
                    btnView.ID = "cmdView" + gvList.Rows[i].Cells[2].Text;
                    btnView.Text = "Skatīt";
                    tc1.Controls.Add(btnView);
                    gvr1.Cells.AddAt(0, tc1);
             */
            foreach (InfoSent MSnt in ilsMsg)
            {
                Table tNest = new Table();
                TableRow trList = new TableRow();
                TableCell tcList = new TableCell();
                TableRow trNest = new TableRow();
                TableCell tcNest = new TableCell();
                Button btnView = new Button();
                btnView.Click += new EventHandler(btnView_Click);
                btnView.ID = "cmdView" + MSnt.ID;
                btnView.CssClass = "cmdEdit";
                btnView.Text = "Skatīt";
                btnView.CommandArgument = MSnt.ID.ToString();
                btnView.PostBackUrl = "#pMsgDetails";
                Label BkmListMSnt = new Label();
                BkmListMSnt.Text = "<a name=\"" + MSnt.ID.ToString() + "\"></a>";
                tcNest.Controls.Add(btnView);
                tcNest.Controls.Add(BkmListMSnt);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                Button btn = new Button();
                btn.Click += new EventHandler(btn_Click);
                btn.ID = "cmdEdit" + MSnt.ID;
                btn.CssClass = "cmdEdit";
                btn.Text = "Labot";
                btn.CommandArgument = MSnt.ID.ToString();
                btn.PostBackUrl = "#pMsg";
                tcNest.Controls.Add(btn);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = "<B>Kam: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MSnt.To + " " + MSnt.Time;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                /*trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Termiņš: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.EndTime.ToString();
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Izpildīts: </B>";
                if (MRcv.CompletedTime.CompareTo(DateTime.Parse("01.01.1900")) > 0)
                {
                    tcNest.Text = tcNest.Text + " " + MRcv.CompletedTime;
                }
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                */
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Ziņojums: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MSnt.Name;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MSnt.Detail;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                /*trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Atskaite: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = MRcv.Report;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                 */
                tcList.Controls.Add(tNest);
                trList.Cells.Add(tcList);
                tList.Rows.Add(trList);
            }
        }
        //nosūtītie uzdevumi
        public void LoadTasksS()
        {
            cmdListMsgIn.CssClass = "cmdTabs";
            cmdListMsgOut.CssClass = "cmdTabs";
            cmdListTasksOut.CssClass = "cmdTabsActive";
            cmdListTasksIn.CssClass = "cmdTabs";

            tList.Visible = true;
            gvList.Visible = false;
            tList.Rows.Clear();
            tlsTask.ConnectionString = GFunc.GetConnectionString("WebCNS");
            tlsTask.LoadData(Session["UserName"].ToString());
            foreach (TaskSent TSnt in tlsTask)
            {
                Table tNest = new Table();
                TableRow trList = new TableRow();
                TableCell tcList = new TableCell();
                TableRow trNest = new TableRow();
                TableCell tcNest = new TableCell();
                //skatīt
                Button btnView = new Button();
                btnView.Click += new EventHandler(btnView_Click);
                btnView.ID = "cmdView" + TSnt.ID;
                btnView.CssClass = "cmdEdit";
                btnView.Text = "Skatīt";
                btnView.CommandArgument = TSnt.ID.ToString();
                btnView.PostBackUrl = "#pTskDetails";
                Label BkmListTSnt = new Label();
                BkmListTSnt.Text = "<a name=\"" + TSnt.ID.ToString() + "\"></a>";
                tcNest.Controls.Add(btnView);
                tcNest.Controls.Add(BkmListTSnt);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                //labot
                tcNest = new TableCell();
                Button cmdEditTask = new Button();
                cmdEditTask.Click += new EventHandler(cmdEditTask_Click);
                cmdEditTask.ID = "cmdEditTask" + TSnt.ID;
                cmdEditTask.CssClass = "cmdEdit";
                cmdEditTask.Text = "Labot";
                cmdEditTask.CommandArgument = TSnt.ID.ToString();
                cmdEditTask.PostBackUrl = "#pTask";
                tcNest.Controls.Add(cmdEditTask);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = "<B>Kam: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TSnt.To + " " + TSnt.Time;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Termiņš: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TSnt.EndTime.ToString();
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Izpildīts: </B>";
                trNest.Cells.Add(tcNest);
                if (TSnt.CompletedTime.CompareTo(DateTime.Parse("01.01.1900")) > 0)
                {
                    tcNest = new TableCell();
                    tcNest.Text = TSnt.CompletedTime.ToString();
                    trNest.Cells.Add(tcNest);
                }
                tcNest.CssClass = "";
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Uzdevums: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TSnt.Name;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TSnt.Detail;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Atskaite: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TSnt.Report;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                tcList.Controls.Add(tNest);
                trList.Cells.Add(tcList);
                tList.Rows.Add(trList);
                /* apakšziņojumus varēs redzēt atsevišķā panelī
                TSnt.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                TSnt.SubTasks.LoadData(Session["UserName"].ToString(), TSnt.ID);
                foreach (SubTaskSent STRcv in TSnt.SubTasks)
                {
                    tNest = new Table();
                    trList = new TableRow();
                    tcList = new TableCell();

                    string[] TPath = STRcv.TaskPath.Split('/');
                    //izveidojam pirmo tukšu aili ar TPath aiļu skaitu
                    trNest = new TableRow();
                    for (int i = 0; i < TPath.GetLength(0); i++)
                    {
                        tcNest = new TableCell();
                        tcNest.Text = "&nbsp;&nbsp;&nbsp;";
                        trNest.Cells.Add(tcNest);
                    }
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "&nbsp;";
                    tcNest.ColumnSpan = TPath.GetLength(0);
                    tcNest.RowSpan = 8;
                    trNest.Cells.Add(tcNest);
                    //trNest = new TableRow();
                    tcNest = new TableCell();
                    Button cmdEditSTask = new Button();
                    cmdEditSTask.Click += new EventHandler(cmdEditSTask_Click);
                    cmdEditSTask.ID = "cmdEditSTask" + TSnt.ID + "-" + STRcv.ID;
                    cmdEditSTask.CssClass = "cmdEdit";
                    cmdEditSTask.Text = "Labot";
                    tcNest.Controls.Add(cmdEditSTask);
                    tcNest.ColumnSpan = TPath.GetLength(0);
                    tcNest.RowSpan = 8;
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Kam: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.To + " " + STRcv.Time;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Termiņš: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.EndTime.ToString();
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Izpildīts: </B>";
                    trNest.Cells.Add(tcNest);
                    if (TSnt.CompletedTime.CompareTo(DateTime.Parse("01.01.1900")) > 0)
                    {
                        tcNest = new TableCell();
                        tcNest.Text = tcNest.Text + " " + STRcv.CompletedTime;
                        trNest.Cells.Add(tcNest);
                    }
                    tcNest.CssClass = "";
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Uzdevums: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = TSnt.Name;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Apakšuzdevums: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Name;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = " ";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Detail;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Atskaite: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = " ";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Report;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    tcList.Controls.Add(tNest);
                    trList.Cells.Add(tcList);
                    tList.Rows.Add(trList);
                }
                */
            }
        }

        void cmdEditSTask_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            string TskCode = b.ID.Substring(12);
            string[] TskID = TskCode.Split('-');
            txtTaskID.Text = TskID[0];
            txtTaskVar.Text = "ST";
            txtSTaskID.Text = TskID[1];
            //txtSTaskPath.Text = "";
            txtRecStatus.Text = "0";
            //txtParentTaskPath.Text = "";
            cbSendTaskByMail.Checked = false;
            cbCompleted.Checked = false;
            lblCompletedTime.Text = string.Empty;
            cmdNewChildTask.Enabled = true;
            cmdDelTask.Enabled = true;
            TaskSent TSent = (TaskSent)tlsTask.Find(Convert.ToInt32(TskID[0]));
            if (TSent != null)
            {
                SubTaskSent STSent = (SubTaskSent)TSent.SubTasks.Find(Convert.ToInt32(TskID[1]));
                if (STSent != null)
                {
                    txtTaskFrom.Text = STSent.From;
                    txtTaskFrom.Enabled = true;
                    txtTaskTo.Text = STSent.To;
                    txtTaskTo.Enabled = true;
                    txtTaskEndTime.Text = STSent.EndTime.ToString("dd.MM.yyyy HH:mm:ss");
                    txtTaskEndTime.Enabled = true;
                    txtTaskName.Text = STSent.Name;
                    txtTaskName.Enabled = true;
                    txtTaskDetail.Text = STSent.Detail;
                    txtTaskDetail.Enabled = true;
                    if (STSent.CompletedTime.CompareTo(Convert.ToDateTime("1900.01.01 00:00:00")) > 0)
                    {
                        cbCompleted.Checked = true;
                        lblCompletedTime.Text = STSent.CompletedTime.ToString("dd.MM.yyyy HH:mm:ss");
                    }
                    txtTaskReport.Text = STSent.Report;
                    txtSTaskPath.Text = STSent.TaskPath;
                    txtParentTaskPath.Text = STSent.ParentTaskPath;
                    //cbCompleted.Enabled = false;
                    //txtTaskReport.Enabled = false;
                    pTask.Visible = true;
                }
            }
        }

        void cmdEditTask_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            string TskID = b.ID.Substring(11);
            txtTaskID.Text = TskID;
            txtTaskVar.Text = "T";
            txtSTaskID.Text = "";
            txtSTaskPath.Text = "";
            txtRecStatus.Text = "0";
            txtParentTaskPath.Text = "";
            cbSendTaskByMail.Checked = false;
            cbCompleted.Checked = false;
            lblCompletedTime.Text = string.Empty;
            cmdNewChildTask.Enabled = true;
            cmdDelTask.Enabled = true;
            TaskSent TSent = (TaskSent)tlsTask.Find(Convert.ToInt32(TskID));
            if (TSent != null)
            {
                txtTaskFrom.Text = TSent.From;
                txtTaskFrom.Enabled = true;
                txtTaskTo.Text = TSent.To;
                txtTaskTo.Enabled = true;
                txtTaskEndTime.Text = TSent.EndTime.ToString("dd.MM.yyyy HH:mm:ss");
                txtTaskEndTime.Enabled = true;
                txtTaskName.Text = TSent.Name;
                txtTaskName.Enabled = true;
                txtTaskDetail.Text = TSent.Detail;
                txtTaskDetail.Enabled = true;
                if (TSent.CompletedTime.CompareTo(Convert.ToDateTime("1900.01.01 00:00:00")) > 0)
                {
                    cbCompleted.Checked = true;
                    lblCompletedTime.Text = TSent.CompletedTime.ToString("dd.MM.yyyy HH:mm:ss");
                }
                txtTaskReport.Text = TSent.Report;
                //cbCompleted.Enabled = false;
                //txtTaskReport.Enabled = false;
                cmdCloseTask.PostBackUrl = "#" + b.CommandArgument;
                pTask.Visible = true;
            }
        }
        //saņemtie uzdevumi
        public void LoadTasksR()
        {
            cmdListMsgIn.CssClass = "cmdTabs";
            cmdListMsgOut.CssClass = "cmdTabs";
            cmdListTasksOut.CssClass = "cmdTabs";
            cmdListTasksIn.CssClass = "cmdTabsActive";

            tList.Visible = true;
            gvList.Visible = false;
            tList.Rows.Clear();
            tlrTask.ConnectionString = GFunc.GetConnectionString("WebCNS");
            tlrTask.LoadData(Session["UserName"].ToString());
            foreach (TaskReceived TRcv in tlrTask)
            {
                if (!TRcv.TaskNotified)
                {
                    TRcv.MarkAsNotified(GFunc.GetConnectionString("WebCNS"), Session["UserName"].ToString());
                } 
                Table tNest = new Table();
                TableRow trList = new TableRow();
                TableCell tcList = new TableCell();
                TableRow trNest = new TableRow();
                TableCell tcNest = new TableCell();
                //skatīt
                Button btnView = new Button();
                btnView.Click += new EventHandler(btnView_Click);
                btnView.ID = "cmdView" + TRcv.ID;
                btnView.CssClass = "cmdEdit";
                btnView.Text = "Skatīt";
                btnView.CommandArgument = TRcv.ID.ToString();
                btnView.PostBackUrl = "#pTskDetails";
                Label BkmListTRcv = new Label();
                BkmListTRcv.Text = "<a name=\"" + TRcv.ID.ToString() + "\"></a>";
                tcNest.Controls.Add(btnView);
                tcNest.Controls.Add(BkmListTRcv);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                //atskaite
                tcNest = new TableCell();
                Button cmdReplyTask = new Button();
                cmdReplyTask.Click += new EventHandler(cmdReplyTask_Click);
                cmdReplyTask.ID = "cmdEditTask" + TRcv.ID;
                cmdReplyTask.CssClass = "cmdReply";
                cmdReplyTask.Text = "Atskaite";
                cmdReplyTask.CommandArgument = TRcv.ID.ToString();
                cmdReplyTask.PostBackUrl = "#pTaskReport";
                tcNest.Controls.Add(cmdReplyTask); 
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = "<B>No: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TRcv.From + " " + TRcv.Time;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Termiņš: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TRcv.EndTime.ToString();
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Izpildīts: </B>";
                trNest.Cells.Add(tcNest);
                if (TRcv.CompletedTime.CompareTo(Convert.ToDateTime("01.01.1900 00:00:00")) > 0)
                {
                    tcNest = new TableCell();
                    tcNest.Text = TRcv.CompletedTime.ToString();
                    trNest.Cells.Add(tcNest);
                }
                tcNest.CssClass = "";
                //trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Uzdevums: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TRcv.Name;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TRcv.Detail;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Atskaite: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TRcv.Report;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                tcList.Controls.Add(tNest);
                if (TRcv.TaskRead)
                {
                    tcList.CssClass = "ReadTask";
                }
                else
                {
                    tcList.CssClass = "UnreadTask";
                }
                trList.Cells.Add(tcList);
                tList.Rows.Add(trList);
                /*apakšuzdevumus varēs redzēt atsevisķā panelī
                TRcv.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                TRcv.SubTasks.LoadData(Session["UserName"].ToString(), TRcv.ID);
                foreach (SubTaskReceived STRcv in TRcv.SubTasks)
                {
                    tNest = new Table();
                    trList = new TableRow();
                    tcList = new TableCell();

                    string[] TPath = STRcv.TaskPath.Split('/');
                    //izveidojam pirmo tukšu aili ar TPath aiļu skaitu
                    trNest = new TableRow();
                    for (int i = 0; i < TPath.GetLength(0); i++)
                    {
                        tcNest = new TableCell();
                        tcNest.Text = "&nbsp;&nbsp;&nbsp;";
                        trNest.Cells.Add(tcNest);
                    }
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "&nbsp;";
                    tcNest.ColumnSpan = TPath.GetLength(0);
                    tcNest.RowSpan = 8;
                    trNest.Cells.Add(tcNest);
                    //trNest = new TableRow();
                    tcNest = new TableCell();
                    Button cmdReplySTask = new Button();
                    cmdReplySTask.Click += new EventHandler(cmdReplySTask_Click);
                    cmdReplySTask.ID = "cmdReplySTask" + TRcv.ID + "-" + STRcv.ID;
                    cmdReplySTask.CssClass = "cmdReply";
                    cmdReplySTask.Text = "Atskaite";
                    tcNest.Controls.Add(cmdReplySTask);
                    tcNest.ColumnSpan = TPath.GetLength(0);
                    tcNest.RowSpan = 8;
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = "<B>No: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.From + " " + STRcv.Time;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Termiņš: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.EndTime.ToString();
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Izpildīts: </B>";
                    trNest.Cells.Add(tcNest);
                    if (STRcv.CompletedTime.CompareTo(Convert.ToDateTime("01.01.1900 00:00:00")) > 0)
                    {
                        tcNest = new TableCell();
                        tcNest.Text = tcNest.Text + " " + STRcv.CompletedTime;
                        trNest.Cells.Add(tcNest);
                    }
                    tcNest.CssClass = "";
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Uzdevums: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = TRcv.Name;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Apakšuzdevums: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Name;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = " ";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Detail;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Atskaite: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = " ";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Report;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    tcList.Controls.Add(tNest);
                    trList.Cells.Add(tcList);
                    tList.Rows.Add(trList);
                }
                */
            }
        }

        void cmdReplySTask_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            string TskCode = b.ID.Substring(13);
            string[] TskID = TskCode.Split('-');
            txtTaskIDR.Text = TskID[0];
            txtTaskVarR.Text = "ST";
            txtSTaskIDR.Text = TskID[1];
            //txtSTaskPath.Text = "";
            txtRecStatusR.Text = "0";
            //txtParentTaskPath.Text = "";
            cbSendTaskByMailR.Checked = false;
            cbCompletedR.Checked = false;
            lblCompletedTimeR.Text = string.Empty;
            TaskReceived TSent = (TaskReceived)tlrTask.Find(Convert.ToInt32(TskID[0]));
            if (TSent != null)
            {
                SubTaskReceived STSent = (SubTaskReceived)TSent.SubTasks.Find(Convert.ToInt32(TskID[1]));
                if (STSent != null)
                {
                    txtTaskFromR.Text = STSent.From;
                    txtTaskToR.Text = STSent.To;
                    txtTaskEndTimeR.Text = STSent.EndTime.ToString("dd.MM.yyyy HH:mm:ss");
                    txtTaskNameR.Text = STSent.Name;
                    txtTaskDetailR.Text = STSent.Detail;
                    if (STSent.CompletedTime.CompareTo(Convert.ToDateTime("1900.01.01 00:00:00")) > 0)
                    {
                        cbCompletedR.Checked = true;
                        lblCompletedTimeR.Text = STSent.CompletedTime.ToString("dd.MM.yyyy HH:mm:ss");
                    }
                    txtTaskReportR.Text = STSent.Report;
                    txtSTaskPathR.Text = STSent.TaskPath;
                    pTaskReport.Visible = true;
                }
            }
            upTskDetails.Update();
        }

        void cmdReplyTask_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            string TskID = b.ID.Substring(11);
            txtTaskIDR.Text = TskID;
            txtTaskVarR.Text = "T";
            txtSTaskIDR.Text = "";
            txtSTaskPathR.Text = "";
            txtRecStatusR.Text = "0";
            txtParentTaskPathR.Text = "";
            cbSendTaskByMailR.Checked = false;
            cbCompletedR.Checked = false;
            lblCompletedTimeR.Text = string.Empty;
            TaskReceived TSent = (TaskReceived)tlrTask.Find(Convert.ToInt32(TskID));
            if (TSent != null)
            {
                if (!TSent.TaskRead)
                {
                    TSent.MarkAsRead(GFunc.GetConnectionString("WebCNS"), Session["UserName"].ToString());
                }
                txtTaskFromR.Text = TSent.From;
                txtTaskToR.Text = TSent.To;
                txtTaskEndTimeR.Text = TSent.EndTime.ToString("dd.MM.yyyy HH:mm:ss");
                txtTaskNameR.Text = TSent.Name;
                txtTaskDetailR.Text = TSent.Detail;
                if (TSent.CompletedTime.CompareTo(Convert.ToDateTime("1900.01.01 00:00:00")) > 0)
                {
                    cbCompletedR.Checked = true;
                    lblCompletedTimeR.Text = TSent.CompletedTime.ToString("dd.MM.yyyy HH:mm:ss");
                }
                txtTaskReportR.Text = TSent.Report;
                cmdCloseTaskR.PostBackUrl = "#" + b.CommandArgument;
                pTaskReport.Visible = true;
            }
            upTskDetails.Update();
        }




        protected void gvList_DataBound(object sender, EventArgs e)
        {
            GridViewRow gvhr = gvList.HeaderRow;
            TableHeaderCell thc = new TableHeaderCell();
            if (ActiveTab == 0)
            {
                thc.Text = "Skatīt";
                gvhr.Cells.AddAt(0, thc);
                //gvhr = gvList.HeaderRow;
            }
            if (ActiveTab == 1)
            {
                thc.Text = "Labot";
                gvhr.Cells.AddAt(0, thc);
                //gvhr = gvList.HeaderRow;
                thc = new TableHeaderCell();
                thc.Text = "Skatīt";
                gvhr.Cells.AddAt(0, thc);

            }
            for (int i = 0; i < gvList.Rows.Count; i++)
            {
                if (ActiveTab == 0 | ActiveTab == 2)
                {
                    GridViewRow gvr1 = gvList.Rows[i];
                    TableCell tc1 = new TableCell();
                    Button btnView = new Button();
                    btnView.Click += new EventHandler(btnView_Click);
                    btnView.ID = "cmdView" + gvList.Rows[i].Cells[1].Text;
                    btnView.Text = "Skatīt";
                    tc1.Controls.Add(btnView);
                    gvr1.Cells.AddAt(0, tc1);
                }
                if (ActiveTab == 1 | ActiveTab == 3)
                {
                    //labot
                    GridViewRow gvr = gvList.Rows[i];
                    TableCell tc = new TableCell();
                    Button btn = new Button();
                    btn.Click += new EventHandler(btn_Click);
                    btn.ID = "cmdEdit" + gvList.Rows[i].Cells[1].Text;
                    btn.Text = "Labot";
                    tc.Controls.Add(btn);
                    gvr.Cells.AddAt(0, tc);
                    //skatīt
                    GridViewRow gvr1 = gvList.Rows[i];
                    TableCell tc1 = new TableCell();
                    Button btnView = new Button();
                    btnView.Click += new EventHandler(btnView_Click);
                    btnView.ID = "cmdView" + gvList.Rows[i].Cells[2].Text;
                    btnView.Text = "Skatīt";
                    tc1.Controls.Add(btnView);
                    gvr1.Cells.AddAt(0, tc1);
                }
                /*gvList.HeaderRow.Cells[2].Visible = false;
                gvList.Rows[i].Cells[2].Visible = false;
                */
                
            }
            gvList.GridLines = GridLines.Both;
            //ja nepieciešams paslēpt aili, daram to šeit
            int a = gvList.Columns.Count;
        }
        
        void btn_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            string InfoID = b.ID.Substring(7);
            if (ActiveTab == 1)
            {
                InfoSent InfS = (InfoSent)ilsMsg.Find(Convert.ToInt32(InfoID));
                if (InfS != null)
                {
                    gvAccessU.DataSource = GFunc.GetData("SELECT User, LongName FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
                    gvAccessU.DataBind();
                    txtID.Text = InfoID;
                    txtTo.Text = InfS.To;
                    txtName.Text = InfS.Name;
                    txtDetail.Text = InfS.Detail;
                    chbSendMailMsg.Checked = false;
                    cmdCloseMsg.PostBackUrl = "#" + b.CommandArgument;
                    pMsg.Visible = !pMsg.Visible;
                }
            }

        }

        void btnView_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            string InfoID = b.ID.Substring(7);
            txtMsgID.Text = InfoID;
            txtMsgType.Text = ActiveTab.ToString();
            ViewedTaskID = InfoID;
            //attīram kontrolus
            tDetails.Rows.Clear();
            txtReply.Text = string.Empty;
            chbReplyAll.Checked = false;
            chbSendMail.Checked = false;
            lblErrDetail.Text = string.Empty;
            //saņemtie ziņojumi
            if (ActiveTab == 0)
            {
                InfoReceived InfR = (InfoReceived)ilrMsg.Find(Convert.ToInt32(InfoID));
                if (InfR != null)
                {
                    if (!InfR.MsgRead)
                    {
                        InfR.MarkAsRead(GFunc.GetConnectionString("WebCNS"), Session["UserName"].ToString());
                    }
                    TableRow tr = new TableRow();
                    TableCell tc = new TableCell();
                    Label lblCell = new Label();
                    TextBox txtCell = new TextBox();
                    lblCell.Text = "Sūtītājs: ";
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = InfR.From + " " + InfR.Time;
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    tr = new TableRow();
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = "Tēma";
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = InfR.Name;
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    tr = new TableRow();
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = "Ziņojums";
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    /*txtCell = new TextBox();
                    txtCell.Text = InfR.Detail;
                    txtCell.CssClass = "FieldsTextReadOnly";
                    txtCell.ReadOnly = true;
                    txtCell.TextMode = TextBoxMode.MultiLine;
                    txtCell.Rows = 3;
                    tc.Controls.Add(txtCell);*/
                    tc.Text = InfR.Detail.Replace("\r\n", "<br>");
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    //pievienojam tukšu rindu
                    tr = new TableRow();
                    tc = new TableCell();
                    tc.Text = "&nbsp";
                    tc.BackColor = System.Drawing.Color.LightSkyBlue;
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = "&nbsp";
                    tc.BackColor = System.Drawing.Color.LightSkyBlue;
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    InfR.SubItems.ConnectionString = ilrMsg.ConnectionString;
                    InfR.SubItems.LoadData(Session["UserName"].ToString(), InfR.ID);
                    pMsgDetails.Height = Unit.Pixel(500);
                    if (InfR.SubItems.Count > 0)
                    {
                        foreach (SubInfoReceived SIR in InfR.SubItems)
                        {
                            tr = new TableRow();
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = "Sūtītājs: ";
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = SIR.From + " " + SIR.Time;
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            tr = new TableRow();
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = "Tēma";
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = SIR.Name;
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            tr = new TableRow();
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = "Atbilde";
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            /*txtCell = new TextBox();
                            txtCell.Text = SIR.Detail;
                            txtCell.ReadOnly = true;
                            txtCell.TextMode = TextBoxMode.MultiLine;
                            txtCell.Rows = 3;
                            txtCell.CssClass = "FieldsTextReadOnly";
                            tc.Controls.Add(txtCell);*/
                            tc.Text = SIR.Detail.Replace("\r\n", "<br>");
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            //pievienojam tukšu rindu
                            tr = new TableRow();
                            tc = new TableCell();
                            tc.Text = "&nbsp";
                            tc.BackColor = System.Drawing.Color.LightSkyBlue;
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            tc.Text = "&nbsp";
                            tc.BackColor = System.Drawing.Color.LightSkyBlue;
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            double PanelH = pMsgDetails.Height.Value;
                            PanelH += 200;
                            pMsgDetails.Height = Unit.Pixel((int)PanelH);
                        }
                    }
                    tr = new TableRow();
                    tDetails.Rows.Add(tr);
                    tDetails.Rows[tDetails.Rows.Count - 1].BorderStyle = BorderStyle.Solid;
                }
                cmdCloseDetail.PostBackUrl = "#" + b.CommandArgument;
                pMsgDetails.Visible = true;
                upDetails.Update();
            }
            //nosūtītie ziņojumi
            if (ActiveTab == 1)
            {
                InfoSent InfS = (InfoSent)ilsMsg.Find(Convert.ToInt32(InfoID));
                if (InfS != null)
                {
                    TableRow tr = new TableRow();
                    TableCell tc = new TableCell();
                    Label lblCell = new Label();
                    TextBox txtCell = new TextBox();
                    lblCell.Text = "Sūtītājs: ";
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = InfS.From + " " + InfS.Time;
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    tr = new TableRow();
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = "Tēma";
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = InfS.Name;
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    tr = new TableRow();
                    tc = new TableCell();
                    lblCell = new Label();
                    lblCell.Text = "Ziņojums";
                    lblCell.CssClass = "LabelFieldCaption";
                    tc.Controls.Add(lblCell);
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    /*txtCell = new TextBox();
                    txtCell.Text = InfS.Detail;
                    txtCell.ReadOnly = true;
                    txtCell.TextMode = TextBoxMode.MultiLine;
                    txtCell.Rows = 3;
                    txtCell.CssClass = "FieldsTextReadOnly"; 
                    tc.Controls.Add(txtCell);*/
                    tc.Text = InfS.Detail.Replace("\r\n","<br>");
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    tr = new TableRow();
                    tc = new TableCell();
                    tc.Text = "&nbsp";
                    tc.BackColor = System.Drawing.Color.LightSkyBlue;
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = "&nbsp";
                    tc.BackColor = System.Drawing.Color.LightSkyBlue;
                    tr.Cells.Add(tc);
                    tDetails.Rows.Add(tr);
                    InfS.SubItems.ConnectionString = ilsMsg.ConnectionString;
                    InfS.SubItems.LoadData(Session["UserName"].ToString(),Convert.ToInt32(InfS.ID));
                    pMsgDetails.Height = Unit.Pixel(500);
                    if (InfS.SubItems.Count > 0)
                    {
                        foreach (SubInfoSent SIS in InfS.SubItems)
                        {
                            tr = new TableRow();
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = "Sūtītājs: ";
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = SIS.From + " " + SIS.Time;
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            tr = new TableRow();
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = "Tēma";
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = SIS.Name;
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            tr = new TableRow();
                            tc = new TableCell();
                            lblCell = new Label();
                            lblCell.Text = "Atbilde";
                            lblCell.CssClass = "LabelFieldCaption";
                            tc.Controls.Add(lblCell);
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            /*txtCell = new TextBox();
                            txtCell.Text = SIS.Detail;
                            txtCell.ReadOnly = true;
                            txtCell.TextMode = TextBoxMode.MultiLine;
                            txtCell.CssClass = "FieldsTextReadOnly";
                            txtCell.Rows = 3;
                            tc.Controls.Add(txtCell);*/
                            tc.Text = SIS.Detail.Replace("\r\n", "<br>");
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            tr = new TableRow();
                            tc = new TableCell();
                            tc.Text = "&nbsp";
                            tc.BackColor = System.Drawing.Color.LightSkyBlue;
                            tr.Cells.Add(tc);
                            tc = new TableCell();
                            tc.Text = "&nbsp";
                            tc.BackColor = System.Drawing.Color.LightSkyBlue;
                            tr.Cells.Add(tc);
                            tDetails.Rows.Add(tr);
                            double PanelH = pMsgDetails.Height.Value;
                            PanelH += 200;
                            pMsgDetails.Height = Unit.Pixel((int)PanelH);
                        }
                        tDetails.CssClass = "TableTaskDetails";
                    }
                    tr = new TableRow();
                    tDetails.Rows.Add(tr);
                    tDetails.Rows[tDetails.Rows.Count - 1].BorderStyle = BorderStyle.Solid;

                }
                cmdCloseDetail.PostBackUrl = "#" + b.CommandArgument;
                pMsgDetails.Visible = true;
                upDetails.Update();
            }
            //saņemtie uzdevumi
            if (ActiveTab == 2)
            {
                
                ViewReceivedTask(Convert.ToInt32(InfoID));
                cmdCloseTskDet.PostBackUrl = "#" + b.CommandArgument;
                pTskDetails.Visible = true;
                upTskDetails.Update();
            }
            //nosūtītie uzdevumi
            if (ActiveTab == 3)
            {
                ViewSentTask(Convert.ToInt32(InfoID));
                cmdCloseTskDet.PostBackUrl = "#" + b.CommandArgument;
                pTskDetails.Visible = true;
                upTskDetails.Update();
            }
            
        }

        protected void ViewSentTask(int TaskID)
        {
            TaskSent TskS = (TaskSent)tlsTask.Find(TaskID);
            if (TskS != null)
            {
                tTskDetails.Rows.Clear();

                Table tNest = new Table();
                TableRow trList = new TableRow();
                TableCell tcList = new TableCell();
                TableRow trNest = new TableRow();
                TableCell tcNest = new TableCell();
                /*//skatīt
                Button btnView = new Button();
                btnView.Click += new EventHandler(btnView_Click);
                btnView.ID = "cmdView" + TskS.ID;
                btnView.CssClass = "cmdEdit";
                btnView.Text = "Skatīt";
                tcNest.Controls.Add(btnView);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                */
                //labot
                tcNest = new TableCell();
                Button cmdEditTask = new Button();
                cmdEditTask.Click += new EventHandler(cmdEditTask_Click);
                cmdEditTask.ID = "cmdEditTasV" + TskS.ID;
                cmdEditTask.CssClass = "cmdEdit";
                cmdEditTask.Text = "Labot";
                tcNest.Controls.Add(cmdEditTask);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = "<B>Kam: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskS.To + " " + TskS.Time;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Termiņš: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskS.EndTime.ToString();
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Izpildīts: </B>";
                trNest.Cells.Add(tcNest);
                if (TskS.CompletedTime.CompareTo(DateTime.Parse("01.01.1900")) > 0)
                {
                    tcNest = new TableCell();
                    tcNest.Text = TskS.CompletedTime.ToString();
                    trNest.Cells.Add(tcNest);
                }
                tcNest.CssClass = "";
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Uzdevums: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskS.Name;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskS.Detail;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Atskaite: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskS.Report;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                tcList.Controls.Add(tNest);
                trList.Cells.Add(tcList);
                tTskDetails.Rows.Add(trList);
                //pievienojam tukšu rindu
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                tc.Text = "&nbsp";
                tc.BackColor = System.Drawing.Color.LightSkyBlue;
                tr.Cells.Add(tc);
                tc = new TableCell();
                tc.Text = "&nbsp";
                tc.BackColor = System.Drawing.Color.LightSkyBlue;
                tr.Cells.Add(tc);
                tTskDetails.Rows.Add(tr);
                TskS.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                TskS.SubTasks.LoadData(Session["UserName"].ToString(), TskS.ID);
                pTskDetails.Height = tTskDetails.Height;

                foreach (SubTaskSent STRcv in TskS.SubTasks)
                {
                    tNest = new Table();
                    trList = new TableRow();
                    tcList = new TableCell();

                    string[] TPath = STRcv.TaskPath.Split('/');
                    //izveidojam pirmo tukšu aili ar TPath aiļu skaitu
                    trNest = new TableRow();
                    for (int i = 0; i < TPath.GetLength(0); i++)
                    {
                        tcNest = new TableCell();
                        tcNest.Text = "&nbsp;&nbsp;&nbsp;";
                        trNest.Cells.Add(tcNest);
                    }
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "&nbsp;";
                    tcNest.ColumnSpan = TPath.GetLength(0);
                    tcNest.RowSpan = 8;
                    trNest.Cells.Add(tcNest);
                    //trNest = new TableRow();
                    tcNest = new TableCell();
                    Button cmdEditSTask = new Button();
                    cmdEditSTask.Click += new EventHandler(cmdEditSTask_Click);
                    cmdEditSTask.ID = "cmdEditSTask" + TskS.ID + "-" + STRcv.ID;
                    cmdEditSTask.CssClass = "cmdEdit";
                    cmdEditSTask.Text = "Labot";
                    tcNest.Controls.Add(cmdEditSTask);
                    tcNest.ColumnSpan = TPath.GetLength(0);
                    tcNest.RowSpan = 8;
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Kam: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.To + " " + STRcv.Time;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Termiņš: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.EndTime.ToString();
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Izpildīts: </B>";
                    trNest.Cells.Add(tcNest);
                    if (TskS.CompletedTime.CompareTo(DateTime.Parse("01.01.1900")) > 0)
                    {
                        tcNest = new TableCell();
                        tcNest.Text = tcNest.Text + " " + STRcv.CompletedTime;
                        trNest.Cells.Add(tcNest);
                    }
                    tcNest.CssClass = "";
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Uzdevums: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = TskS.Name;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Apakšuzdevums: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Name;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = " ";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Detail;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = "<B>Atskaite: </B>";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    trNest = new TableRow();
                    tcNest = new TableCell();
                    tcNest.Text = " ";
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tcNest = new TableCell();
                    tcNest.Text = STRcv.Report;
                    tcNest.CssClass = "";
                    trNest.Cells.Add(tcNest);
                    tNest.Rows.Add(trNest);
                    tcList.Controls.Add(tNest);
                    trList.Cells.Add(tcList);
                    tTskDetails.Rows.Add(trList);
                    //pievienojam tukšu rindu
                    tr = new TableRow();
                    tc = new TableCell();
                    tc.Text = "&nbsp";
                    tc.BackColor = System.Drawing.Color.LightSkyBlue;
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = "&nbsp";
                    tc.BackColor = System.Drawing.Color.LightSkyBlue;
                    tr.Cells.Add(tc);
                    tTskDetails.Rows.Add(tr);
                    double PanelH = pTskDetails.Height.Value;
                    PanelH += 500;
                    pTskDetails.Height = Unit.Pixel((int)PanelH);
                }
                tr = new TableRow();
                tTskDetails.Rows.Add(tr);
                tTskDetails.Rows[tTskDetails.Rows.Count - 1].BorderStyle = BorderStyle.Solid;
            }
        }

        //procedūra saņemtā uzdevuma apskatei formā
        protected void ViewReceivedTask(int TaskID)
        {
            TaskReceived TskR = (TaskReceived)tlrTask.Find(TaskID);
            if (TskR != null)
            {
                if (!TskR.TaskRead)
                {
                    TskR.MarkAsRead(GFunc.GetConnectionString("WebCNS"), Session["UserName"].ToString());
                }
                tTskDetails.Rows.Clear();

                Table tNest = new Table();
                TableRow trList = new TableRow();
                TableCell tcList = new TableCell();
                TableRow trNest = new TableRow();
                TableCell tcNest = new TableCell();

                //atskaite
                tcNest = new TableCell();
                Button cmdReplyTask = new Button();
                cmdReplyTask.Click += new EventHandler(cmdReplyTask_Click);
                cmdReplyTask.ID = "cmdEditTasV" + TskR.ID;
                cmdReplyTask.CssClass = "cmdReply";
                cmdReplyTask.Text = "Atskaite";
                tcNest.Controls.Add(cmdReplyTask);
                tcNest.ColumnSpan = 0;
                tcNest.RowSpan = 7;
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = "<B>No: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskR.From + " " + TskR.Time;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Termiņš: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskR.EndTime.ToString();
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Izpildīts: </B>";
                trNest.Cells.Add(tcNest);
                if (TskR.CompletedTime.CompareTo(Convert.ToDateTime("01.01.1900 00:00:00")) > 0)
                {
                    tcNest = new TableCell();
                    tcNest.Text = TskR.CompletedTime.ToString();
                    trNest.Cells.Add(tcNest);
                }
                tcNest.CssClass = "";
                //trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Uzdevums: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskR.Name;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskR.Detail;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = "<B>Atskaite: </B>";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                trNest = new TableRow();
                tcNest = new TableCell();
                tcNest.Text = " ";
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tcNest = new TableCell();
                tcNest.Text = TskR.Report;
                tcNest.CssClass = "";
                trNest.Cells.Add(tcNest);
                tNest.Rows.Add(trNest);
                tcList.Controls.Add(tNest);
                trList.Cells.Add(tcList);
                tTskDetails.Rows.Add(trList);
                //pievienojam tukšu rindu
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                tc.Text = "&nbsp";
                tc.BackColor = System.Drawing.Color.LightSkyBlue;
                tr.Cells.Add(tc);
                tc = new TableCell();
                tc.Text = "&nbsp";
                tc.BackColor = System.Drawing.Color.LightSkyBlue;
                tr.Cells.Add(tc);
                tTskDetails.Rows.Add(tr);
                TskR.SubTasks.ConnectionString = tlrTask.ConnectionString;
                TskR.SubTasks.LoadData(Session["UserName"].ToString(), TskR.ID);
                pTskDetails.Height = Unit.Pixel(1000);
                if (TskR.SubTasks.Count > 0)
                {
                    foreach (SubTaskReceived STR in TskR.SubTasks)
                    {
                        tNest = new Table();
                        trList = new TableRow();
                        tcList = new TableCell();

                        string[] TPath = STR.TaskPath.Split('/');
                        //izveidojam pirmo tukšu aili ar TPath aiļu skaitu
                        trNest = new TableRow();
                        for (int i = 0; i < TPath.GetLength(0); i++)
                        {
                            tcNest = new TableCell();
                            tcNest.Text = "&nbsp;&nbsp;&nbsp;";
                            trNest.Cells.Add(tcNest);
                        }
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = "&nbsp;";
                        tcNest.ColumnSpan = TPath.GetLength(0);
                        tcNest.RowSpan = 8;
                        trNest.Cells.Add(tcNest);
                        //trNest = new TableRow();
                        tcNest = new TableCell();
                        Button cmdReplySTask = new Button();
                        cmdReplySTask.Click += new EventHandler(cmdReplySTask_Click);
                        cmdReplySTask.ID = "cmdReplySTask" + TskR.ID + "-" + STR.ID;
                        cmdReplySTask.CssClass = "cmdReply";
                        cmdReplySTask.Text = "Atskaite";
                        tcNest.Controls.Add(cmdReplySTask);
                        tcNest.ColumnSpan = TPath.GetLength(0);
                        tcNest.RowSpan = 8;
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = "<B>No: </B>";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = STR.From + " " + STR.Time;
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = "<B>Termiņš: </B>";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = STR.EndTime.ToString();
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = "<B>Izpildīts: </B>";
                        trNest.Cells.Add(tcNest);
                        if (STR.CompletedTime.CompareTo(Convert.ToDateTime("01.01.1900 00:00:00")) > 0)
                        {
                            tcNest = new TableCell();
                            tcNest.Text = tcNest.Text + " " + STR.CompletedTime;
                            trNest.Cells.Add(tcNest);
                        }
                        tcNest.CssClass = "";
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = "<B>Uzdevums: </B>";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = TskR.Name;
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = "<B>Apakšuzdevums: </B>";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = STR.Name;
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = " ";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = STR.Detail;
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = "<B>Atskaite: </B>";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        trNest = new TableRow();
                        tcNest = new TableCell();
                        tcNest.Text = " ";
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tcNest = new TableCell();
                        tcNest.Text = STR.Report;
                        tcNest.CssClass = "";
                        trNest.Cells.Add(tcNest);
                        tNest.Rows.Add(trNest);
                        tcList.Controls.Add(tNest);
                        trList.Cells.Add(tcList);
                        tTskDetails.Rows.Add(trList);
                        //pievienojam tukšu rindu
                        tr = new TableRow();
                        tc = new TableCell();
                        tc.Text = "&nbsp";
                        tc.BackColor = System.Drawing.Color.LightSkyBlue;
                        tr.Cells.Add(tc);
                        tc = new TableCell();
                        tc.Text = "&nbsp";
                        tc.BackColor = System.Drawing.Color.LightSkyBlue;
                        tr.Cells.Add(tc);
                        tTskDetails.Rows.Add(tr);
                        double PanelH = pTskDetails.Height.Value;
                        PanelH += 500;
                        pTskDetails.Height = Unit.Pixel((int)PanelH);
                    }
                }
                tr = new TableRow();
                tTskDetails.Rows.Add(tr);
                tTskDetails.Rows[tTskDetails.Rows.Count - 1].BorderStyle = BorderStyle.Solid;
            }
        }

        protected void cmdListMsgIn_Click(object sender, EventArgs e)
        {
            ActiveTab = 0;
            pTskDetails.Visible = false;
            pTaskReport.Visible = false;
            pMsg.Visible = false;
            pTask.Visible = false;
            pMsgDetails.Visible = false;
            LoadMsgR();
        }

        protected void cmdListMsgOut_Click(object sender, EventArgs e)
        {
            ActiveTab = 1;
            pTskDetails.Visible = false;
            pTaskReport.Visible = false;
            pMsg.Visible = false;
            pTask.Visible = false;
            pMsgDetails.Visible = false;
            LoadMsgS();
        }

        protected void cmdListTasksIn_Click(object sender, EventArgs e)
        {
            ActiveTab = 2;
            pTskDetails.Visible = false;
            pTaskReport.Visible = false;
            pMsg.Visible = false;
            pTask.Visible = false;
            pMsgDetails.Visible = false;
            LoadTasksR();
        }

        protected void cmdListTasksOut_Click(object sender, EventArgs e)
        {
            ActiveTab = 3;
            pTskDetails.Visible = false;
            pTaskReport.Visible = false;
            pMsg.Visible = false;
            pTask.Visible = false;
            pMsgDetails.Visible = false;
            LoadTasksS();
        }

        protected void cmdCloseDetail_Click(object sender, EventArgs e)
        {
            lblErrDetail.Text = "";
            pMsgDetails.Visible = false;
            ViewedTaskID = null;
            upDetails.Update();
            upList.Update();
        }

        protected void cmdReply_Click(object sender, EventArgs e)
        {
            string UserList;
            lblErrDetail.Text = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(txtReply.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīta atbilde.");
                    
                }
                if (txtMsgType.Text == "0")//saņemtās ziņas
                {
                    
                    InfoReceived InfR = (InfoReceived)ilrMsg.Find(Convert.ToInt32(txtMsgID.Text));
                    //int ParentMsgID = Convert.ToInt32(txtMsgID.Text);
                    if (chbReplyAll.Checked == true)
                    {
                        UserList = InfR.From + ";" + InfR.To;
                    }
                    else
                    {
                        UserList = InfR.From;
                    }
                    InfR.SubItems.ConnectionString = ilrMsg.ConnectionString;
                    InfR.SubItems.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                    InfR.Reply(Session["UserName"].ToString(), UserList, GFunc.EscapeSQLCahrs(txtReply.Text), chbSendMail.Checked);

                }
                if (txtMsgType.Text == "1")//nosūtītās ziņas
                {
                    InfoSent InfS = (InfoSent)ilsMsg.Find(Convert.ToInt32(txtMsgID.Text));
                    //int ParentMsgID = Convert.ToInt32(txtMsgID.Text);
                    if (chbReplyAll.Checked == true)
                    {
                        UserList = InfS.From + ";" + InfS.To;
                    }
                    else
                    {
                        UserList = InfS.From;
                    }
                    InfS.SubItems.ConnectionString = ilsMsg.ConnectionString;
                    InfS.SubItems.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                    InfS.Reply(Session["UserName"].ToString(), UserList,GFunc.EscapeSQLCahrs(txtReply.Text), chbSendMail.Checked);

                }
                if (txtMsgType.Text == "2")//saņemtie uzdevumi
                {

                }
                if (txtMsgType.Text == "3")//nosūtītie uzdevumi
                {

                }
                //pMsgDetails.Visible = false;
                lblErrDetail.Text = "Atbilde nosūtīta";
                upDetails.Update();
            }
            catch(Exception ex)
            {
                lblErrDetail.Text = ex.Message;
            }

            
        }

        protected void cmdShowUser_Click(object sender, EventArgs e)
        {
            gvAccessU.Visible = !gvAccessU.Visible;
        }

        protected void cmdNewMsg_Click(object sender, EventArgs e)
        {
            gvAccessU.DataSource = GFunc.GetData("SELECT User, LongName FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
            gvAccessU.DataBind();
            txtID.Text = "0";
            txtTo.Text = "";
            txtName.Text = "";
            txtDetail.Text = "";
            chbSendMailMsg.Checked = false;
            pMsg.Visible = !pMsg.Visible;
        }

        protected void gvAccessU_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtTo.Text.Contains(";" + gvAccessU.SelectedRow.Cells[1].Text + ";"))
            {
                txtTo.Text = txtTo.Text.Replace(";" + gvAccessU.SelectedRow.Cells[1].Text + ";", ";");
            }
            else
            {
                txtTo.Text = txtTo.Text + ";" + gvAccessU.SelectedRow.Cells[1].Text + ";";
                txtTo.Text = txtTo.Text.Replace(";;", ";");
            }

        }

        protected void cmdCloseMsg_Click(object sender, EventArgs e)
        {
            lblErrMsg.Text = "";
            pMsg.Visible = false;
            upList.Update();
        }

        protected void cmdDelMsg_Click(object sender, EventArgs e)
        {
            
            ilsMsg.Remove(ilsMsg.Find(Convert.ToInt32(txtID.Text)));
            lblErrMsg.Text = "Ziņojums izdzēsts.";
        }

        protected void cmdSendMsg_Click(object sender, EventArgs e)
        {
            try
            {
                lblErrMsg.Text = string.Empty;

                if (string.IsNullOrEmpty(txtDetail.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīts ziņojums.");

                }
                if (string.IsNullOrEmpty(txtTo.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīts adresāts.");

                }
                if (string.IsNullOrEmpty(txtName.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīta tēma.");

                }
                InfoSent InfS = new InfoSent();
                if (txtID.Text == "0")
                {
                    InfS.CompletedTime = DateTime.Now;
                    InfS.Detail = GFunc.EscapeSQLCahrs(txtDetail.Text);
                    InfS.EndTime = DateTime.Now;
                    InfS.From = Session["UserName"].ToString();
                    InfS.ID = Convert.ToInt32(txtID.Text);
                    InfS.Name = GFunc.EscapeSQLCahrs(txtName.Text);
                    InfS.RecStatus = 0;
                    InfS.Time = DateTime.Now;
                    InfS.To = txtTo.Text;

                }
                else
                {
                    InfS = (InfoSent)ilsMsg.Find(Convert.ToInt32(txtID.Text));
                    if (InfS != null)
                    {
                        InfS.CompletedTime = DateTime.Now;
                        InfS.Detail = GFunc.EscapeSQLCahrs(txtDetail.Text);
                        InfS.EndTime = DateTime.Now;
                        InfS.From = Session["UserName"].ToString();
                        InfS.ID = Convert.ToInt32(txtID.Text);
                        InfS.Name = GFunc.EscapeSQLCahrs(txtName.Text);
                        InfS.RecStatus = 0;
                        InfS.Time = DateTime.Now;
                        InfS.To = txtTo.Text;
                    }
                }
                ilsMsg.ConnectionString = GFunc.GetConnectionString("WebCNS");
                ilsMsg.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                ilsMsg.Add(InfS,chbSendMailMsg.Checked);
                lblErrMsg.Text = "Ziņojums nosūtīts.";
            }
            catch (Exception ex)
            {
                lblErrMsg.Text = ex.Message;
                
            }
        }

        protected void gvTaskTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtTaskTo.Text.Contains(";" + gvTaskTo.SelectedRow.Cells[1].Text + ";"))
            {
                txtTaskTo.Text = txtTaskTo.Text.Replace(";" + gvTaskTo.SelectedRow.Cells[1].Text + ";", ";");
            }
            else
            {
                txtTaskTo.Text = txtTaskTo.Text + ";" + gvTaskTo.SelectedRow.Cells[1].Text + ";";
                txtTaskTo.Text = txtTaskTo.Text.Replace(";;", ";");
            }
        }

        protected void cmdTaskTo_Click(object sender, EventArgs e)
        {
            gvTaskTo.Visible = !gvTaskTo.Visible;
        }

        protected void cmdTaskEndTime_Click(object sender, EventArgs e)
        {
            if (txtTaskEndTime.Text.Trim().Length > 0)
            {
                cTaskEndTime.SelectedDate = Convert.ToDateTime(txtTaskEndTime.Text);
            }
            cTaskEndTime.Visible = !cTaskEndTime.Visible;
        }

        protected void cTaskEndTime_SelectionChanged(object sender, EventArgs e)
        {
            txtTaskEndTime.Text = cTaskEndTime.SelectedDate.ToString("dd.MM.yyyy") + " 23:59:59";
            cTaskEndTime.Visible = false;
        }

        protected void cbCompleted_CheckedChanged(object sender, EventArgs e)
        {
            if (cbCompleted.Checked == true)
            {
                lblCompletedTime.Text = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
            }
            else
            {
                lblCompletedTime.Text = "";
            }
        }

        protected void cbCompletedR_CheckedChanged(object sender, EventArgs e)
        {
            if (cbCompletedR.Checked == true)
            {
                lblCompletedTimeR.Text = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
            }
            else
            {
                lblCompletedTimeR.Text = "";
            }
        }

        protected void cmdCloseTask_Click(object sender, EventArgs e)
        {
            lblTaskError.Text = "";
            pTask.Visible = false;
            upList.Update();
            

        }

        protected void cmdNew_Click(object sender, EventArgs e)
        {
            lblTaskError.Text = string.Empty;
            txtTaskVar.Text = "T";
            gvTaskTo.DataSource = GFunc.GetData("SELECT User, LongName FROM users WHERE RecStatus=0 ORDER BY User", "WebCNS", false);
            gvTaskTo.DataBind();
            txtTaskID.Text = "0";
            txtSTaskID.Text = "0";
            txtTaskFrom.Text = string.Empty;
            txtTaskTo.Text = string.Empty;
            txtTaskEndTime.Text = string.Empty;
            txtTaskName.Text = string.Empty;
            txtTaskDetail.Text = string.Empty;
            cbSendTaskByMail.Checked = false;
            cbCompleted.Checked = false;
            txtTaskReport.Text = string.Empty;
            txtRecStatus.Text = "0";
            txtSTaskPath.Text = string.Empty;
            cmdNewChildTask.Enabled = true;
            cmdDelTask.Enabled = true;

            pTask.Visible = true;
        }

        protected void cmdDelTask_Click(object sender, EventArgs e)
        {
            try
            {
                lblTaskError.Text = "";
                if (txtTaskVar.Text == "T")
                {
                    tlsTask.Remove(tlsTask.Find(Convert.ToInt32(txtTaskID.Text)));
                }
                else
                {
                    SubTaskListSent STL = tlsTask.Find(Convert.ToInt32(txtTaskID.Text)).SubTasks;
                    STL.Remove(STL.Find(Convert.ToInt32(txtSTaskID.Text)));

                }
                lblTaskError.Text = "Uzdevums izdzēsts.";
                upDetails.Update();
            }
            catch(Exception ex)
            {
                lblTaskError.Text = ex.Message;
            }
        }

        protected void cmdSaveTask_Click(object sender, EventArgs e)
        {
            bool IsNewTask = false;

            try
            {
                lblTaskError.Text = string.Empty;

                if (string.IsNullOrEmpty(txtTaskDetail.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīts uzdevums.");

                }
                if (string.IsNullOrEmpty(txtTaskTo.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīts adresāts.");

                }
                if (string.IsNullOrEmpty(txtTaskName.Text.Trim()) == true)
                {
                    throw new ApplicationException("Nav ievadīts nosaukums.");

                }
                if (txtTaskVar.Text == "T")
                {
                    TaskSent TaskS = new TaskSent();
                    if (txtTaskID.Text != "0")
                    {
                        TaskS = (TaskSent)tlsTask.Find(Convert.ToInt32(txtTaskID.Text));
                        if (TaskS.ID == 0)
                        {
                            lblTaskError.Text = "Uzdevums nav atrasts.";
                            return;
                        }
                    }
                    else
                    {
                        IsNewTask = true;
                    }

                    TaskS.Detail = GFunc.EscapeSQLCahrs(txtTaskDetail.Text);
                    TaskS.EndTime = Convert.ToDateTime(txtTaskEndTime.Text);
                    TaskS.From = Session["UserName"].ToString();
                    TaskS.ID = Convert.ToInt32(txtTaskID.Text);
                    TaskS.Name = GFunc.EscapeSQLCahrs(txtTaskName.Text);
                    TaskS.RecStatus = 0;
                    TaskS.Time = DateTime.Now;
                    TaskS.To = GFunc.EscapeSQLCahrs(txtTaskTo.Text);
                    if (lblCompletedTime.Text.Equals(string.Empty))
                    {
                        TaskS.CompletedTime = Convert.ToDateTime("1900.01.01 00:00:00");
                    }
                    else
                    {
                        TaskS.CompletedTime = Convert.ToDateTime(lblCompletedTime.Text);
                    }
                    TaskS.Report = GFunc.EscapeSQLCahrs(txtTaskReport.Text);
                    tlsTask.ConnectionString = GFunc.GetConnectionString("WebCNS");
                    tlsTask.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                    tlsTask.Add(TaskS, cbSendTaskByMail.Checked);
                    //ja ir norādīti vairāki saņēmēji, izveidojam katram no tiem savu apakšuzdevumu
                    //un šis ir jauns ieraksts
                    string[] Recipients = TaskS.To.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
                    if (Recipients.Length > 1 & IsNewTask == true)
                    {
                        for (int i = 0; i < Recipients.Length; i++)
                        {
                            SubTaskSent SubTaskS = new SubTaskSent();

                            SubTaskS.Detail = GFunc.EscapeSQLCahrs(txtTaskDetail.Text);
                            SubTaskS.EndTime = Convert.ToDateTime(txtTaskEndTime.Text);
                            SubTaskS.From = Session["UserName"].ToString();
                            SubTaskS.ID = 0;
                            SubTaskS.Name = GFunc.EscapeSQLCahrs(txtTaskName.Text);
                            SubTaskS.RecStatus = 0;
                            SubTaskS.Time = DateTime.Now;
                            SubTaskS.To = GFunc.EscapeSQLCahrs(Recipients[i]);
                            if (lblCompletedTime.Text.Equals(string.Empty))
                            {
                                SubTaskS.CompletedTime = Convert.ToDateTime("1900.01.01 00:00:00");
                            }
                            else
                            {
                                SubTaskS.CompletedTime = Convert.ToDateTime(lblCompletedTime.Text);
                            }
                            SubTaskS.Report = GFunc.EscapeSQLCahrs(txtTaskReport.Text);
                            SubTaskS.TaskPath = GFunc.EscapeSQLCahrs(txtSTaskPath.Text);
                            SubTaskS.PTaskID = Convert.ToInt32(txtTaskID.Text);
                            SubTaskS.ParentTaskPath = TaskS.ID.ToString() + "/";
                            TaskS.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                            TaskS.SubTasks.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                            TaskS.SubTasks.Add(SubTaskS, false);
                        }
                    }
                }
                else
                {
                    TaskSent TaskS = new TaskSent();
                    if (txtTaskID.Text == "0")
                    {
                        lblTaskError.Text = "Nav norādīts pamatuzdevuma ID.";
                        return;
                    }
                    else
                    {
                        TaskS = (TaskSent)tlsTask.Find(Convert.ToInt32(txtTaskID.Text));
                        if (TaskS.ID == 0)
                        {
                            lblTaskError.Text = "Pamatuzdevums nav atrasts.";
                            return;
                        }
                    }
                    SubTaskSent SubTaskS = new SubTaskSent();
                    if (txtSTaskID.Text != "0")
                    {
                        SubTaskS = (SubTaskSent)TaskS.SubTasks.Find(Convert.ToInt32(txtSTaskID.Text));
                        if (SubTaskS.ID == 0)
                        {
                            lblTaskError.Text = "Apakšuzdevums nav atrasts.";
                            return;
                        }
                    }
                    else
                    {
                        IsNewTask = true;
                    }
                    SubTaskS.Detail = GFunc.EscapeSQLCahrs(txtTaskDetail.Text);
                    SubTaskS.EndTime = Convert.ToDateTime(txtTaskEndTime.Text);
                    SubTaskS.From = Session["UserName"].ToString();
                    SubTaskS.ID = Convert.ToInt32(txtSTaskID.Text);
                    SubTaskS.Name = GFunc.EscapeSQLCahrs(txtTaskName.Text);
                    SubTaskS.RecStatus = 0;
                    SubTaskS.Time = DateTime.Now;
                    SubTaskS.To = GFunc.EscapeSQLCahrs(txtTaskTo.Text);
                    if (lblCompletedTime.Text.Equals(string.Empty))
                    {
                        SubTaskS.CompletedTime = Convert.ToDateTime("1900.01.01 00:00:00");
                    }
                    else
                    {
                        SubTaskS.CompletedTime = Convert.ToDateTime(lblCompletedTime.Text);
                    }
                    SubTaskS.Report = GFunc.EscapeSQLCahrs(txtTaskReport.Text);
                    SubTaskS.TaskPath = GFunc.EscapeSQLCahrs(txtSTaskPath.Text);
                    SubTaskS.PTaskID = Convert.ToInt32(txtTaskID.Text);
                    SubTaskS.ParentTaskPath = GFunc.EscapeSQLCahrs(txtParentTaskPath.Text);
                    TaskS.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                    TaskS.SubTasks.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                    TaskS.SubTasks.Add(SubTaskS, cbSendTaskByMail.Checked);
                    //ja ir norādīti vairāki saņēmēji, izveidojam katram no tiem savu apakšuzdevumu
                    //un šis ir jauns ieraksts
                    string[] Recipients = TaskS.To.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (Recipients.Length > 1 & IsNewTask == true)
                    {
                        for (int i = 0; i < Recipients.Length; i++)
                        {
                            SubTaskSent SubSubTaskS = new SubTaskSent();

                            SubSubTaskS.Detail = GFunc.EscapeSQLCahrs(txtTaskDetail.Text);
                            SubSubTaskS.EndTime = Convert.ToDateTime(txtTaskEndTime.Text);
                            SubSubTaskS.From = Session["UserName"].ToString();
                            SubSubTaskS.ID = 0;
                            SubSubTaskS.Name = GFunc.EscapeSQLCahrs(txtTaskName.Text);
                            SubSubTaskS.RecStatus = 0;
                            SubSubTaskS.Time = DateTime.Now;
                            SubSubTaskS.To = GFunc.EscapeSQLCahrs(Recipients[i]);
                            if (lblCompletedTime.Text.Equals(string.Empty))
                            {
                                SubSubTaskS.CompletedTime = Convert.ToDateTime("1900.01.01 00:00:00");
                            }
                            else
                            {
                                SubSubTaskS.CompletedTime = Convert.ToDateTime(lblCompletedTime.Text);
                            }
                            SubSubTaskS.Report = GFunc.EscapeSQLCahrs(txtTaskReport.Text);
                            SubSubTaskS.TaskPath = GFunc.EscapeSQLCahrs(txtSTaskPath.Text);
                            SubSubTaskS.PTaskID = Convert.ToInt32(txtTaskID.Text);
                            SubSubTaskS.ParentTaskPath = GFunc.EscapeSQLCahrs(SubTaskS.ParentTaskPath);
                            TaskS.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                            TaskS.SubTasks.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                            TaskS.SubTasks.Add(SubSubTaskS, false);
                        }
                    }
                }
                lblTaskError.Text = "Uzdevums saglabāts.";
                upList.Update();
                //pTask.Visible = false;

            }
            catch (Exception ex)
            {

                lblTaskError.Text = ex.Message;
            }

        }

        protected void cmdNewChildTask_Click(object sender, EventArgs e)
        {
            txtTaskID.Text = txtTaskID.Text;//nemainās
            txtTaskVar.Text = "ST";
            txtSTaskID.Text = "0";
            //txtSTaskPath.Text = "";
            txtRecStatus.Text = "0";
            //txtParentTaskPath.Text = "";
            cbSendTaskByMail.Checked = false;
            txtTaskFrom.Text = Session["UserName"].ToString();
            txtTaskFrom.Enabled = true;
            txtTaskTo.Text = txtTaskTo.Text;//nemainās
            txtTaskTo.Enabled = true;
            txtTaskEndTime.Text = string.Empty;
            txtTaskEndTime.Enabled = true;
            txtTaskName.Text = string.Empty;
            txtTaskName.Enabled = true;
            txtTaskDetail.Text = string.Empty;
            txtTaskDetail.Enabled = true;
            cbCompleted.Checked = false;
            lblCompletedTime.Text = string.Empty;
            txtTaskReport.Text = string.Empty;
            txtParentTaskPath.Text = txtSTaskPath.Text;
            txtSTaskPath.Text = string.Empty;
            //cbCompleted.Enabled = false;
            //txtTaskReport.Enabled = false;
        }

        protected void cmdCloseTaskR_Click(object sender, EventArgs e)
        {
            lblTaskErrorR.Text = "";
            pTaskReport.Visible = false;
            upList.Update();
            
        }

        protected void cmdSaveTaskR_Click(object sender, EventArgs e)
        {
            bool IsNewTask = false;

            try
            {
                lblTaskErrorR.Text = string.Empty;

                if (txtTaskVarR.Text == "T")
                {
                    TaskReceived TaskS = new TaskReceived();
                    if (txtTaskIDR.Text != "0")
                    {
                        TaskS = (TaskReceived)tlrTask.Find(Convert.ToInt32(txtTaskIDR.Text));
                        if (TaskS.ID == 0)
                        {
                            lblTaskErrorR.Text = "Uzdevums nav atrasts.";
                            return;
                        }
                    }
                    else
                    {
                        IsNewTask = true;
                    }

                    if (lblCompletedTimeR.Text.Equals(string.Empty))
                    {
                        TaskS.CompletedTime = Convert.ToDateTime("1900.01.01 00:00:00");
                    }
                    else
                    {
                        TaskS.CompletedTime = Convert.ToDateTime(lblCompletedTimeR.Text);
                    }
                    TaskS.Report = GFunc.EscapeSQLCahrs(txtTaskReportR.Text);
                    tlrTask.ConnectionString = GFunc.GetConnectionString("WebCNS");
                    tlrTask.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                    tlrTask.AddReport(TaskS, cbSendTaskByMailR.Checked);
                }
                else
                {
                    TaskReceived TaskS = new TaskReceived();
                    if (txtTaskIDR.Text == "0")
                    {
                        lblTaskErrorR.Text = "Nav norādīts pamatuzdevuma ID.";
                        return;
                    }
                    else
                    {
                        TaskS = (TaskReceived)tlrTask.Find(Convert.ToInt32(txtTaskIDR.Text));
                        if (TaskS.ID == 0)
                        {
                            lblTaskErrorR.Text = "Pamatuzdevums nav atrasts.";
                            return;
                        }
                    }
                    SubTaskReceived SubTaskS = new SubTaskReceived();
                    if (txtSTaskIDR.Text != "0")
                    {
                        SubTaskS = (SubTaskReceived)TaskS.SubTasks.Find(Convert.ToInt32(txtSTaskIDR.Text));
                        if (SubTaskS.ID == 0)
                        {
                            lblTaskErrorR.Text = "Apakšuzdevums nav atrasts.";
                            return;
                        }
                    }
                    else
                    {
                        IsNewTask = true;
                    }

                    if (lblCompletedTimeR.Text.Equals(string.Empty))
                    {
                        SubTaskS.CompletedTime = Convert.ToDateTime("1900.01.01 00:00:00");
                    }
                    else
                    {
                        SubTaskS.CompletedTime = Convert.ToDateTime(lblCompletedTimeR.Text);
                    }
                    SubTaskS.Report = GFunc.EscapeSQLCahrs(txtTaskReportR.Text);
                    TaskS.SubTasks.ConnectionString = GFunc.GetConnectionString("WebCNS");
                    TaskS.SubTasks.SMTPServer = GlobalF.AppOptions["SMTPServer"];
                    TaskS.SubTasks.AddReport(SubTaskS, cbSendTaskByMailR.Checked);

                }
                lblTaskErrorR.Text = "Uzdevums saglabāts.";
                upList.Update();
                //pTaskReport.Visible = false;

            }
            catch (Exception ex)
            {

                lblTaskErrorR.Text = ex.Message;
            }

        }

        protected void cmdCloseTskDet_Click(object sender, EventArgs e)
        {
            pTskDetails.Visible = false;
            ViewedTaskID = null;
            upTskDetails.Update();
            upList.Update();
        }


        protected void cmdShowOpt_Click(object sender, EventArgs e)
        {
            
            pOptions.Visible = !pOptions.Visible;
            if (pOptions.Visible == true)
            {
                
                if (ActiveTab == 0)
                {
                    ViewOptions("MsgReceived");
                    //OptionStr = ilrMsg.GetUser2MsgOptions(Session["UserName"].ToString(), "MsgReceived", GFunc.GetConnectionString("WebCNS"));
                    
                    
                }
                if (ActiveTab == 1)
                {
                    ViewOptions("MsgSent");
                    //OptionStr = ilsMsg.GetUser2MsgOptions(Session["UserName"].ToString(), "MsgSent", GFunc.GetConnectionString("WebCNS"));
                    

                }
                if (ActiveTab == 2)
                {
                    ViewOptions("TskReceived");
                    //OptionStr = tlrTask.GetUser2MsgOptions(Session["UserName"].ToString(), "TskReceived", GFunc.GetConnectionString("WebCNS"));
                    

                }
                if (ActiveTab == 3)
                {
                    ViewOptions("TskSent");
                    //OptionStr = tlsTask.GetUser2MsgOptions(Session["UserName"].ToString(), "TskSent", GFunc.GetConnectionString("WebCNS"));
                    

                }
                
            }
        }

        void cmdDelOrder_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            //string InfoID = b.ID.Substring(7);
            foreach (TableRow tRow in tSort.Rows)
            {
                foreach (Control rCtrl in tRow.Cells[0].Controls)
                {
                    if(rCtrl.ID.Equals(b.ID))
                    {
                        txtOpOrder.Text = txtOpOrder.Text.Replace("" + tRow.Cells[1].Text.Trim() + " " + tRow.Cells[2].Text.Trim(), "");
                        if (txtOpOrder.Text.StartsWith(","))
                            txtOpOrder.Text = txtOpOrder.Text.Remove(0, 1);
                        if (txtOpOrder.Text.StartsWith(" ,"))
                            txtOpOrder.Text = txtOpOrder.Text.Remove(0, 2);
                        if (txtOpOrder.Text.EndsWith(","))
                            txtOpOrder.Text = txtOpOrder.Text.Remove(txtOpOrder.Text.Length-1, 1);

                        break;
                    }
                }
            }
            string[] OptionStr = { "", "" };
            OptionStr[0] = txtOpWhere.Text;
            OptionStr[1] = txtOpOrder.Text;
            if (ActiveTab == 0)
            {
                ilrMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 1)
            {
                ilsMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 2)
            {
                tlrTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 3)
            {
                tlsTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            ViewOptions(txtMsgTypeFilt.Text);
        }

        void cmdDelFilt_Click(object sender, EventArgs e)
        {
            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            //string InfoID = b.ID.Substring(7);
            foreach (TableRow tRow in tFilters.Rows)
            {
                foreach (Control rCtrl in tRow.Cells[0].Controls)
                {
                    if (rCtrl.ID.Equals(b.ID))
                    {
                        txtOpWhere.Text = txtOpWhere.Text.Replace(" AND " + tRow.Cells[1].Text.Trim() + " " + tRow.Cells[2].Text.Trim() + " " + tRow.Cells[3].Text.Trim(), "");
                        break;
                    }
                }
            }
            string[] OptionStr = { "", "" };
            OptionStr[0] = txtOpWhere.Text;
            OptionStr[1] = txtOpOrder.Text;
            if (ActiveTab == 0)
            {
                ilrMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 1)
            {
                ilsMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 2)
            {
                tlrTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 3)
            {
                tlsTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            ViewOptions(txtMsgTypeFilt.Text);
        }

        protected void cmdCloseOpt_Click(object sender, EventArgs e)
        {
            pOptions.Visible = false;
            upOptions.Update();
            upList.Update();
            
        }

        protected void cmdAddFilt_Click(object sender, EventArgs e)
        {
            //ViewOptions(txtMsgTypeFilt.Text);
            /*string qrFields = "SELECT * FROM options WHERE OptType='User' AND OptGroup='" + txtMsgTypeFilt.Text + "' AND OptUser='" + Session["UserName"].ToString() + "' AND OptName='WHERE'";
            DataSet dsFields = GFunc.GetData(qrFields, "WebCNS", false);
            if (dsFields.Tables.Count > 0)
            {
                if (dsFields.Tables[0].Rows.Count > 0)
                {

                }
            }*/
            string[] OptionStr = { "", "" };
            txtOpWhere.Text = txtOpWhere.Text + " AND " + ddlFieldFilt.SelectedValue + " " + ddlDarbFilt.SelectedValue + " " + txtValueFilt.Text;
            
            OptionStr[0] = txtOpWhere.Text;
            OptionStr[1] = txtOpOrder.Text;
            if (ActiveTab == 0)
            {
                ilrMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 1)
            {
                ilsMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 2)
            {
                tlrTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 3)
            {
                tlsTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            /*
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            Button cmdDelFilt = new Button();
            cmdDelFilt.ID = "cmdDelF" + tFilters.Rows.Count;
            cmdDelFilt.CssClass = "cmdDel";
            cmdDelFilt.Text = "Dzēst";
            cmdDelFilt.Click += new EventHandler(cmdDelFilt_Click);
            tc.Controls.Add(cmdDelFilt);
            tc.Width = Unit.Percentage(10);
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = ddlFieldFilt.SelectedValue;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = ddlDarbFilt.SelectedValue;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = txtValueFilt.Text;
            tr.Cells.Add(tc);
            tFilters.Rows.Add(tr);
            double PanelH = pFilters.Height.Value;
            PanelH += 10;
            pFilters.Height = Unit.Pixel((int)PanelH);
             */
            ViewOptions(txtMsgTypeFilt.Text);
        }

        protected void cmdAddSort_Click(object sender, EventArgs e)
        {
            string[] OptionStr = { "", "" };
            
            if (txtOpOrder.Text.Trim().Length > 0)
            {
                txtOpOrder.Text = txtOpOrder.Text + ", " + ddlFieldSort.SelectedValue + " " + ddlDarbSort.SelectedValue;
            }
            else
            {
                txtOpOrder.Text = " " + ddlFieldSort.SelectedValue + " " + ddlDarbSort.SelectedValue;
            }
            OptionStr[0] = txtOpWhere.Text;
            OptionStr[1] = txtOpOrder.Text;
            if (ActiveTab == 0)
            {
                ilrMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 1)
            {
                ilsMsg.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 2)
            {
                tlrTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            if (ActiveTab == 3)
            {
                tlsTask.SaveUser2MsgOptions(OptionStr, Session["UserName"].ToString(), txtMsgTypeFilt.Text, GFunc.GetConnectionString("WebCNS"));
            }
            ViewOptions(txtMsgTypeFilt.Text);

            /*
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            Button cmdDelOrder = new Button();
            cmdDelOrder.ID = "cmdDelO" + tSort.Rows.Count;
            cmdDelOrder.CssClass = "cmdDel";
            cmdDelOrder.Text = "Dzēst";
            cmdDelOrder.Click += new EventHandler(cmdDelOrder_Click);
            tc.Controls.Add(cmdDelOrder);
            tc.Width = Unit.Percentage(10);
            tr.Cells.Add(tc);
            //string[] OrderRec = OrderStr[i].Split(new string[] { " LIKE ", "<>", "<=", ">=", "=", "<", ">" }, StringSplitOptions.RemoveEmptyEntries);
       
            tc = new TableCell();
            tc.Text = ddlFieldSort.SelectedValue;
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Text = ddlDarbSort.SelectedValue;
            tr.Cells.Add(tc);
            tSort.Rows.Add(tr);
            double PanelH = pSort.Height.Value;
            PanelH += 10;
            pSort.Height = Unit.Pixel((int)PanelH);
             */
        }

        protected void cmdSaveOpt_Click(object sender, EventArgs e)
        {

        }

        protected void ViewOptions(string MessageType)
        {
            string[] OptionStr;
            string[] FilterStr = { "" };
            string[] OrderStr = { "" };
            double PanelH;
            txtMsgTypeFilt.Text = MessageType;
            tFilters.Rows.Clear();
            tSort.Rows.Clear();
            OptionStr = tlsTask.GetUser2MsgOptions(Session["UserName"].ToString(), MessageType, GFunc.GetConnectionString("WebCNS"));
            txtOpWhere.Text = OptionStr[0];
            txtOpOrder.Text = OptionStr[1];
            FilterStr = OptionStr[0].Split(new string[] { " AND " }, StringSplitOptions.RemoveEmptyEntries);
            OrderStr = OptionStr[1].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            //filtru ielāde
            for (int i = 0; i < FilterStr.Length; i++)
            {
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                Button cmdDelFilt = new Button();
                cmdDelFilt.ID = "cmdDelF" + i;
                cmdDelFilt.CssClass = "cmdDel";
                cmdDelFilt.Text = "Dzēst";
                cmdDelFilt.Click += new EventHandler(cmdDelFilt_Click);
                tc.Controls.Add(cmdDelFilt);
                tc.Width = Unit.Percentage(10);
                tr.Cells.Add(tc);
                string Operator = string.Empty;
                if (FilterStr[i].Contains(" LIKE ") == true & Operator == string.Empty)
                    Operator = "LIKE";
                if (FilterStr[i].Contains("<>") == true & Operator == string.Empty)
                    Operator = "<>";
                if (FilterStr[i].Contains("<=") == true & Operator == string.Empty)
                    Operator = "<=";
                if (FilterStr[i].Contains(">=") == true & Operator == string.Empty)
                    Operator = ">=";
                if (FilterStr[i].Contains("=") == true & Operator == string.Empty)
                    Operator = "=";
                if (FilterStr[i].Contains("<") == true & Operator == string.Empty)
                    Operator = "<";
                if (FilterStr[i].Contains(">") == true & Operator == string.Empty)
                    Operator = ">";

                string[] FilterRec = FilterStr[i].Split(new string[] { " LIKE ", "<>", "<=", ">=", "=", "<", ">" }, StringSplitOptions.RemoveEmptyEntries);
                if (FilterRec.Length > 1)
                {
                    tc = new TableCell();
                    tc.Text = FilterRec[0];
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = Operator;
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = FilterRec[1];
                    tr.Cells.Add(tc);
                    tFilters.Rows.Add(tr);
                    PanelH = pFilters.Height.Value;
                    PanelH += 10;
                    pFilters.Height = Unit.Pixel((int)PanelH);
                    PanelH = pOptions.Height.Value;
                    PanelH += 10;
                    pOptions.Height = Unit.Pixel((int)PanelH);
                }
            }
            //šķirošanas ielāde
            for (int i = 0; i < OrderStr.Length; i++)
            {
                if (OrderStr[i].Trim().Length < 3)
                    break;
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                Button cmdDelOrder = new Button();
                cmdDelOrder.ID = "cmdDelO" + i;
                cmdDelOrder.CssClass = "cmdDel";
                cmdDelOrder.Text = "Dzēst";
                cmdDelOrder.Click += new EventHandler(cmdDelOrder_Click);
                tc.Controls.Add(cmdDelOrder);
                tc.Width = Unit.Percentage(10);
                tr.Cells.Add(tc);
                //string[] OrderRec = OrderStr[i].Split(new string[] { " LIKE ", "<>", "<=", ">=", "=", "<", ">" }, StringSplitOptions.RemoveEmptyEntries);
                if (OrderStr[i].EndsWith(" ASC") == true)
                {
                    tc = new TableCell();
                    tc.Text = OrderStr[i].Replace(" ASC", "");
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = "ASC";
                    tr.Cells.Add(tc);
                }
                if (OrderStr[i].EndsWith(" DESC") == true)
                {
                    tc = new TableCell();
                    tc.Text = OrderStr[i].Replace(" DESC", "");
                    tr.Cells.Add(tc);
                    tc = new TableCell();
                    tc.Text = "DESC";
                    tr.Cells.Add(tc);
                }
                tSort.Rows.Add(tr);
                PanelH = pSort.Height.Value;
                PanelH += 10;
                pSort.Height = Unit.Pixel((int)PanelH);
                PanelH = pOptions.Height.Value;
                PanelH += 10;
                pOptions.Height = Unit.Pixel((int)PanelH);
            }
            
        }

        protected void cmdSaveTskDet_Click(object sender, EventArgs e)
        {

        }


    }
}
